package com.shwread.android.qysw.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.shwread.android.qysw10000152.R;
import com.shwread.android.ui.dialog.NoNetDialog;
import com.shwread.android.ui.widget.xlistview.XListView;
import com.shwread.android.ui.widget.xlistview.XListView.IXListViewListener;
import com.shwread.http.ActionHelp;
import com.shwread.http.base.ObjectHttpResponseHandler;
import org.apache.http.Header;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import logic.bean.ColumDetailGame;
import logic.bean.GameInfos;
import logic.constants.DefaultConsts;
import logic.util.ImageLoaderUtil;
import logic.util.NetworkUtil;


public class ColumnGameMoreActivity
        extends
        BaseBusinessActivity
        implements
        OnClickListener,
        IXListViewListener
{
	private int             pageNum = 1;
	private int             columnId;
	private String          columnName;
	private TextView        tvTitle;
	private Button          btnBack;
	private XListView       lv;
	
	//	private ColumDetailGame columDetailGame;
	//	private boolean         hasMore = true;
	private List<GameInfos> allData = new ArrayList<GameInfos>();
	private Adapter         adapter = new Adapter();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		columnId = intent.getIntExtra("columnId", 1);
		columnName = intent.getStringExtra("columnName");
		getData();
		findViews();
		
	}
	
	private void getData() {
		if (!NetworkUtil.isConnected(this))
		{
			new NoNetDialog(ColumnGameMoreActivity.this).show();
			return;
		}
		ActionHelp.qryGamesByColumnId(this, columnId, pageNum, DefaultConsts.DEFAULT_PAGE_LENGTH, new GetGameListHandler());
	}
	
	private class GetGameListHandler
	        extends
	        ObjectHttpResponseHandler<ColumDetailGame>
	{
		
		@Override
		public Type getType() {
			return new TypeToken<ColumDetailGame>()
			{
			}.getType();
		}
		
		@Override
		public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
			//			getUIHandler().sendEmptyMessage(-1);
			
		}
		
		@Override
		public void onSuccess(int statusCode, Header[] headers, ColumDetailGame obj, String responseString) {
			System.out.println("data" + obj.toString());
			lv.stopLoadMore();
			lv.stopRefresh();
			if (pageNum == 1)
			{
				lv.setRefreshTime(new SimpleDateFormat("MM-dd HH:mm:ss", Locale.CHINA).format(System.currentTimeMillis()));
				allData.clear();
			}
			if (obj.getGameInfoList() != null)
			{
				if (obj.getTotalPage() <= pageNum)
				{
					lv.setPullLoadEnable(false);
				}
				else
				{
					lv.setPullLoadEnable(true);
				}
				allData.addAll(obj.getGameInfoList());
			}
			pageNum++;
			
			adapter.notifyDataSetChanged();
		}
		
	}
	
	private void findViews() {
		setContentView(R.layout.column_game_more);
		tvTitle = (TextView) findViewById(R.id.common_header_name);
		tvTitle.setText(columnName);
		btnBack = (Button) findViewById(R.id.common_header_back);
		btnBack.setVisibility(View.VISIBLE);
		btnBack.setOnClickListener(this);
		lv = (XListView) findViewById(R.id.column_detail_listview);
		lv.setVisibility(View.VISIBLE);
		lv.setXListViewListener(this);
		adapter.notifyDataSetChanged();
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new OnItemClickListener()
		{
			
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent webIntent = new Intent(ColumnGameMoreActivity.this, GameWebActivity.class);
				webIntent.putExtra(DefaultConsts.title_s, allData.get(position - 1).getGameName());
				webIntent.putExtra(DefaultConsts.req_url_s, allData.get(position - 1).getUrl());
				//webIntent.putExtra(DefaultConsts.req_url_s, "http://201591614.fangshui123.com/game/games/ljsp/");
				startActivity(webIntent);
			}
		});
	}
	
	private class Adapter
	        extends
	        BaseAdapter
	{
		
		@Override
		public int getCount() {
			return allData.size();
		}
		
		@Override
		public Object getItem(int position) {
			return allData.get(position);
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public View getView(int position, View view, ViewGroup parent) {
			GameInfos game = (GameInfos) getItem(position);
			view = LayoutInflater.from(ColumnGameMoreActivity.this).inflate(R.layout.boutique_game_list_item, null);
			ImageView gameImg = (ImageView) view.findViewById(R.id.boutique_game_img);
			TextView gameName = (TextView) view.findViewById(R.id.boutique_game_name);
			TextView gameIntro = (TextView) view.findViewById(R.id.boutique_game_intro);
			
			ImageLoaderUtil.loadWebUrl(game.getPicUrl(), gameImg, ImageLoaderUtil.getLowOptions(R.drawable.boutique_fl_img));
			gameName.setText(game.getGameName());
			gameIntro.setText(game.getIntro());
			return view;
		}
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_header_back:
				onBackPressed();
				break;
			default:
				break;
		}
	}
	
	@Override
	public Handler getUIHandler() {
		return null;
	}
	
	@Override
	public void onRefresh() {
		pageNum = 1;
		getData();
	}
	
	@Override
	public void onLoadMore() {
		getData();
	}
	
}
